﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;

namespace AKJ.Sharepoint.Branding.Config
{
    [ConfigurationCollection(typeof(ApplyMasterElement), AddItemName = "ApplyMaster",
      CollectionType = ConfigurationElementCollectionType.BasicMap)]
    public class Collections : ConfigurationElementCollection
    {
        public ApplyMasterElement this[int index]
        {
            get { return (ApplyMasterElement)BaseGet(index); }
            set
            {
                if (BaseGet(index) != null)
                    BaseRemoveAt(index);
                BaseAdd(index, value);
            }
        }

        public void Add(ApplyMasterElement element)
        {
            BaseAdd(element);
        }

        public void Clear()
        {
            BaseClear();
        }

        protected override string ElementName
        {
            get
            {
                return "ApplyMaster";
            }
        }

        protected override ConfigurationElement CreateNewElement()
        {
            return new ApplyMasterElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((ApplyMasterElement)element).File;
        }

        public void Remove(ApplyMasterElement element)
        {
            BaseRemove(element.File);
        }

        public void Remove(string name)
        {
            BaseRemove(name);
        }

        public void RemoveAt(int index)
        {
            BaseRemoveAt(index);
        }
    }

    [ConfigurationCollection(typeof(BrandingInterceptorElement), AddItemName = "Interceptor",
   CollectionType = ConfigurationElementCollectionType.BasicMap)]
    public class BrandingInterceptorCollection : ConfigurationElementCollection
    {
        public BrandingInterceptorElement this[int index]
        {
            get { return (BrandingInterceptorElement)BaseGet(index); }
            set
            {
                if (BaseGet(index) != null)
                    BaseRemoveAt(index);
                BaseAdd(index, value);
            }
        }

        public void Add(BrandingInterceptorElement element)
        {
            BaseAdd(element);
        }

        public void Clear()
        {
            BaseClear();
        }

        protected override string ElementName
        {
            get
            {
                return "Interceptor";
            }
        }

        protected override ConfigurationElement CreateNewElement()
        {
            return new BrandingInterceptorElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((BrandingInterceptorElement)element).Type;
        }

        public void Remove(BrandingInterceptorElement element)
        {
            BaseRemove(element.Type);
        }

        public void Remove(string name)
        {
            BaseRemove(name);
        }

        public void RemoveAt(int index)
        {
            BaseRemoveAt(index);
        }
    }

    [ConfigurationCollection(typeof(RebrandPathElement), AddItemName = "RebrandPath",
   CollectionType = ConfigurationElementCollectionType.BasicMap)]
    public class RebrandPathCollection : ConfigurationElementCollection
    {
        public RebrandPathElement this[int index]
        {
            get { return (RebrandPathElement)BaseGet(index); }
            set
            {
                if (BaseGet(index) != null)
                    BaseRemoveAt(index);
                BaseAdd(index, value);
            }
        }

        public void Add(RebrandPathElement element)
        {
            BaseAdd(element);
        }

        public void Clear()
        {
            BaseClear();
        }

        protected override string ElementName
        {
            get
            {
                return "RebrandPath";
            }
        }

        protected override ConfigurationElement CreateNewElement()
        {
            return new RebrandPathElement();
        }

        int seed = 1;
        protected override object GetElementKey(ConfigurationElement element)
        {
            return (++seed).ToString();
            //return ((RebrandPathElement)element).Path;
        }

        public void Remove(RebrandPathElement element)
        {
            BaseRemove(element.Path);
        }

        public void Remove(string name)
        {
            BaseRemove(name);
        }

        public void RemoveAt(int index)
        {
            BaseRemoveAt(index);
        }
    }

    [ConfigurationCollection(typeof(QuerystringElement), AddItemName = "QuerystringBranding",
     CollectionType = ConfigurationElementCollectionType.BasicMap)]
    public class QuerystringBrandingCollection : ConfigurationElementCollection
    {
        public QuerystringElement this[int index]
        {
            get { return (QuerystringElement)BaseGet(index); }
            set
            {
                if (BaseGet(index) != null)
                    BaseRemoveAt(index);
                BaseAdd(index, value);
            }
        }

        public void Add(QuerystringElement element)
        {
            BaseAdd(element);
        }

        public void Clear()
        {
            BaseClear();
        }

        protected override string ElementName
        {
            get
            {
                return "QuerystringBranding";
            }
        }

        protected override ConfigurationElement CreateNewElement()
        {
            return new QuerystringElement();
        }
                
        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((QuerystringElement)element).Parameter;
        }

        public void Remove(QuerystringElement element)
        {
            BaseRemove(element.Parameter);
        }

        public void Remove(string name)
        {
            BaseRemove(name);
        }

        public void RemoveAt(int index)
        {
            BaseRemoveAt(index);
        }
    }
}